<?php
class Admin_ControlController extends Zendvn_Controller_Action{
	
	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;
	
	//Duong dan cua Controller
	protected $_currentController;
	
	//Duong dan cua Action chinh
	protected $_actionMain;
	
	//Thong so phan trang
	protected $_paginator = array(
									'itemCountPerPage' => 5,
									'pageRange' => 3,
								  ); 
	protected $_namespace;
	protected $_user;
	
	public function init(){
		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();
		$template_path = TEMPLATE_PATH . "/admin";
		$this->loadTemplate($template_path,'template.ini','template');
	
		$this->_user = new Zend_Session_Namespace ( "user" );
		if (!isset ( $this->_user->username )){

			$this->_redirect ( "admin/" );
		
		}
			$this->view->username =$this->_user->username;
		$this->view->role =$this->_user->role;
	}

	public function indexAction(){
		$dataBase = new Admin_Model_Pages();
		if(!isset($this->_arrParam['id_cate']) ){
			$this->view->active ="1";
			$this->view->id_cate="1";
			if($this->_user->role >1){
				$pages=$dataBase->getPageByUser($this->_user->id );
			}else{
				$pages=$dataBase->getAll();
			}
		}else{
			$this->view->active =$this->_arrParam['id_cate'];
			$pages=$dataBase->getAll($this->_arrParam['id_cate']);
			$this->view->id_cate=$this->_arrParam['id_cate'];
			if($this->_user->role >1){
				$pages=$dataBase->getPageByUser($this->_user->id,$this->_arrParam['id_cate'] );
			}else{
				$pages=$dataBase->getAll($this->_arrParam['id_cate']);
			}
		}
		$pa = Zend_Paginator::factory($pages);
		$pa->setItemCountPerPage(4);// xet so hang tren mọt trang
		$pa->setPageRange(5);
		$current = $this->_request->getParam('page');
		$pa->setCurrentPageNumber($current);
		// phan trang cache du lieu
		$fO = array("lifetime"=>3600, "automatic_serialization"=>true);
		$bO = array("cache_dir"=> APPLICATION_PATH.'/../public/tmp');
		$cache = Zend_Cache::factory('Core', 'File', $fO,$bO);
		Zend_Paginator::setCache($cache);
		$this->view->pages=$pa;
		
	}
	public function addAction() {
	$this->view->active =$this->_arrParam['id_cate'];
	$this->view->id_cate=$this->_arrParam['id_cate'];
	if($this->_request->isPost()){
		
			$pages = new Admin_Model_Pages();
			$this->_arrParam["id_user"]=$this->_user->id;
			$pages->addPage($this->_arrParam );
			
		
	}
		
	}
	public function changeAction(){
		$this->view->active="4";
		if($this->_request->isPost()){
		  if($this->_arrParam['pass1'] == $this->_arrParam['pass2'] ){
			$data= new Admin_Model_Users();
			$user1['username']=$this->_user->username;
			$user1['password']=$this->_arrParam['pass'];
			$user1['passwordnew']=$this->_arrParam['pass1'];
			$result=$data->changePass($user1);
			if($result >0){
				$this->_redirect ( "admin/control" );
			}
			$this->view->error ="2";
		  }else{
		  	 $this->view->error ="1";
		  	
		  }
		  $this->view->pass =$this->_arrParam['pass'];
		  $this->view->pass1 =$this->_arrParam['pass1'];
		  $this->view->pass2 =$this->_arrParam['pass2'];
				
		}
		
		
	}
	public function registerAction() {
		$this->view->active="7";
		$dataBase = new Admin_Model_Register();
			$pages=$dataBase->getAll();
		$pa = Zend_Paginator::factory($pages);
		$pa->setItemCountPerPage(10);// xet so hang tren mọt trang
		$pa->setPageRange(10);
		$current = $this->_request->getParam('page');
		$pa->setCurrentPageNumber($current);
		// phan trang cache du lieu
		$fO = array("lifetime"=>3600, "automatic_serialization"=>true);
		$bO = array("cache_dir"=> APPLICATION_PATH.'/../public/tmp');
		$cache = Zend_Cache::factory('Core', 'File', $fO,$bO);
		Zend_Paginator::setCache($cache);
		$this->view->pages=$pa;
		
	}
	public function viewAction(){
		$dataBase = new Admin_Model_Register();
		$register=$dataBase->getRegister($this->_arrParam);
		$this->view->register=$register;
	}
	public function  editAction(){
		
		$pages = new Admin_Model_Pages();
		if(!$this->_request->isPost()){
			$page=$pages->getPage($this->_arrParam['id']);
			$this->view->active =$page['id_cate'];
			$this->view->page=$page;
			if($this->_user->role>1 && $page['id_user'] !=$this->_user->id){
				$this->_redirect ( "admin/control" );
			}
		}else{
			$pages->editPage($this->_arrParam );
			$this->_redirect ( "admin/control" );
		}
		
	}
	public function deleteAction(){
		$pages = new Admin_Model_Pages();
		$page=$pages->getPage($this->_arrParam['id']);
		if($this->_user->role>1 && $page['id_user'] !=$this->_user->id){
			$this->_redirect ( "admin/control" );
		}
		$page["id"]=$this->_arrParam["id"];
		$pages->deletePage($page);
		$this->_redirect ( "admin/control" );
	}
}
